package com.hisicom.scqxspxcjc.services;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hisicom.scqxspxcjc.common.file.bean.FileBean;
import com.hisicom.scqxspxcjc.common.file.serivce.FileService;
import com.hisicom.scqxspxcjc.interfaces.SignTool;
import com.hisicom.scqxspxcjc.sceneCheck.report.domain.Report;
import com.hisicom.scqxspxcjc.sceneCheck.report.service.ReportService;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.domain.SystemCheck;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.exception.SystemCheckException;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.service.SystemCheckService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author zhonggls
 * @date 2023/9/27 09:23
 * @Explain
 */
@Service
@Transactional
public class SystemCheckServiceBus {
    @Autowired
    private SystemCheckService systemCheckService;
    @Autowired
    private FileService fileService;
    @Autowired
    private ReportService reportService;

    /**
     * 通过账号名获取(未检查)对应体系核查列表
     *
     * @param account 账号名称
     * @return 体系核查集合
     */
    public List<SystemCheck> findAllSystemCheckList(String account) {
        return systemCheckService.findByAccountAndCheckStatus(account);
    }

    public void delById(String fileId) {
        fileService.deleteById(fileId);
    }

    public FileBean findByFileId(String fileId) {
        return fileService.findById(fileId);
    }

    public List<FileBean> findFileByGroupId(String groupId, String label) {
        if (StrUtil.isBlank(label)) {
            return fileService.findByFkBusinessId(groupId);
        } else {
            return fileService.findByFkBusinessIdAndLabel(groupId, label);
        }
    }

    public List<FileBean> findByGroupId(String groupId) {
        return fileService.findByFkBusinessId1(groupId);
    }

    /**
     * 通过获取未上传的数据
     **/
    public Object findByCheckStatus() {
        List<SystemCheck> systemCheckList = systemCheckService.findByCheckStatus("1");
        if (ObjectUtil.isNotEmpty(systemCheckList)) {
            Set<String> groupIdList = new HashSet<>();
            systemCheckList.forEach(systemCheck -> {
                groupIdList.add(systemCheck.getGroupId());
            });
            if (ObjectUtil.isNotEmpty(groupIdList)) {
                List<Report> reportList = reportService.findAllByGroupIdIs(groupIdList);
                return reportList;
            }
        }
        return new ArrayList<>();
    }

    /**
     * 调整状态为：完成上传
     **/
    public void updateCheckStatus(String groupId) {
        systemCheckService.updateCheckStatus(groupId, "2");
    }

    public void finish(String[] groupIds) {
        if (ObjectUtil.isNotEmpty(groupIds)) {
            for (String groupId : groupIds) {
                systemCheckService.updateCheckStatus(groupId, "2");
            }
        }
    }

    public void updateCheckStatus(String[] groupIds, String status) {
        if (ObjectUtil.isNotEmpty(groupIds)) {
            for (String groupId : groupIds) {
                systemCheckService.updateCheckStatus(groupId, status);
            }
        }
    }

    /**
     * 提交检查
     *
     * @param groupId 分组id
     * @return 体系核查对象集合
     */
    public void submit(String groupId) {
        if (StrUtil.isNotBlank(groupId)) {
            String[] groupIds = groupId.split(",");
            for (String groupIdone : groupIds) {
                List<Report> list = reportService.findByGroupId(groupIdone);
                if (ObjectUtil.isEmpty(list)) {
                    throw new SystemCheckException("未生成现场检查报告，请先填写现场检查报告！");
                }
            }
            //检查是否已生成现场检查记录
            updateCheckStatus(groupIds, "1");
        }
    }


    /**
     * 通过组id获取体系核查对象集合
     *
     * @param groupId 分组id
     * @return 体系核查对象集合
     */
    public List<SystemCheck> findAllSystemCheckByGroupId(String groupId) {
        return systemCheckService.findByGroupId(groupId);
    }

    /**
     * 新增检查对象
     *
     * @param systemCheck 检查对象
     */
    public void addSyctemCheck(SystemCheck systemCheck) {
        systemCheckService.addSyctemCheck(systemCheck);
    }

    public Boolean login(@NonNull String account, @NonNull String password) {
        List<SystemCheck> systemCheckList = systemCheckService.findByAccount(account);
        if (systemCheckList.size() == 0) {
            return false;
        } else {
            SystemCheck systemCheck = systemCheckList.get(0);
            String password1 = systemCheck.getPassword();
            String passwordsha1 = SignTool.sha1(password1 + "SC_QXSP");
            if (passwordsha1.equals(password)) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 保存文件
     *
     * @param groupId 分组id
     * @param file    文件
     * @param label   类型 schc:首次核查 ;zghc:整改复查; xcjcjl:现场检查记录 ;bfh:不符合项目情况表;
     * @return
     */
    public void save(String groupId, @RequestParam(value = "file", required = false) MultipartFile file, String label, String name) {
        fileService.saveInfo(groupId, label, name, file);
    }
}
